package first.endtoend

import first.endtoend.Aid
import first.endtoend.Portfolio
import first.endtoend.PortfolioDetail
import first.endtoend.Product

/**
 * this service return a Json Object : portfolio related to one family with its details
 */
class PortfolioService
{

    def serviceMethod()
    {

    }


    def portfolioDetailsService
    def aidService
    def productService

    def toJson(Portfolio portfolio)
    {


        def listResult
        PortfolioDetail detail
        if (portfolio)
        {
            listResult = new ArrayList<PortfolioDetail>()

            portfolio.portfolioDetails.each {
                if (it)
                {
                    listResult.add(portfolioDetailsService.toJson(it))
                } else
                {
                    println("NO portfolio Details!!")
                }

            }
            println detail
            [

                    id     : portfolio.id,
                    // family:familyService.toJson(portfolio.family),
                    details: listResult
            ]

        } else
        {
            //TODO code error 201 idFamily exists but there is no portfolio for this family
            println("there is no portfolio for this family")

        }


    }

    def getPortfolioDetails(Set<PortfolioDetail> list)
    {

        int i = 0
        PortfolioDetail portfolioDetail
        Product product
        Aid aid
        while (list != null)
        {
            portfolioDetail = list.get(i)
            product = portfolioDetail.product
            aid = portfolioDetail.aid
            [
                    id                : portfolioDetail.id,
                    aid               : aidService.toJson(aid),
                    quantity          : portfolioDetail.quantity,
                    reduction         : portfolioDetail.reduction,
                    productName       : product.name,
                    productDescription: product.description,
                    product           : productService.toJson(product)

            ]
            i++
        }

    }
}
